<script>
    import ActionList from "../base/ActionList.svelte";
    import FullPathData from "../../eventbus/FullPathData.js";
    import {onMount} from "svelte";
    import MapManager from "../../scripts/maps/MapManager.js";
    import action from "../../scripts/character/action.js";
    import {mapEventBus} from "../../eventbus/map_signal.js";

    export let where;
    let mapManager;

    onMount(() => {
        mapManager = new MapManager(window)
    })

    let items = [{
        text: "连续移动",
        cost: 2,
        description: "移动到此地， 点击非绿色区域结束移动",
        callback() {
            console.log(`将从${JSON.stringify(FullPathData.source)}移动到${JSON.stringify(FullPathData.target)}`)
            let source = FullPathData.source
            let target = FullPathData.target
            mapManager = new MapManager(window) // 这里可能数据会刷新 重置一个m apManager
            let character_source = mapManager.map_data[source.row][source.col].character
            if (character_source) {
                let action_instance = action.init(character_source) // 加载角色
                action_instance.character.where = {
                    col: target.col,
                    row: target.row
                } // 位置更替
                action_instance.save() // 保存角色
                mapEventBus.emit('map-reload', Math.random(), ()=>{
                    FullPathData.source = {
                        col: target.col,
                        row: target.row
                    } // 连续移动
                    return action_instance.get_range_cell()
                }) // 重新加载角色信息
            } else {
                alert(`未找到${JSON.stringify(FullPathData.source)}的角色`)
            }

        }
    }, {
        text: "建筑",
        cost: 2,
        callback() {
        },
        description: "在此地建立建筑",
    }, {
        text: "建立城邦",
        cost: 2,
        callback() {
        },
        description: "在此地建立城邦",
    }]
</script>

<div class="characterActionList">
    <b style="display: block;margin: 0px 4px 10px">
        无主之地({where.col}, {where.row})
    </b>

    <hr/>
    {#each items as item}
        <ActionList {item} {where}/>
    {/each}
</div>

<style lang="less">
  .characterActionList {
    padding: 10px 0;
    box-sizing: border-box;
  }
</style>